#include <os/task.h>
#include <os/string.h>
#include <os/kernel.h>
#include <type.h>

uint64_t load_task_img(int taskid)
{
    /**
     * TODO:
     * 1. [p1-task3] load task from image via task id, and return its entrypoint
     * 2. [p1-task4] load task via task name, thus the arg should be 'char *taskname'
     */
     
    //[p1-task3]
    /*
    int sectorNum = 15;
    int block_id  = 1 + taskid * sectorNum;
    int memAdress = TASK_MEM_BASE + (taskid - 1) * TASK_SIZE;

    if(taskid >= 1 && taskid <= TASK_MAXNUM){
	    bios_sdread(memAdress,sectorNum,block_id);
    }

    return memAdress;*/

    /*
    //[p1-task4]
    int taskid;
    for (taskid = 0; strcmp(taskname,tasks[taskid].task_name) != 0 && taskid < tasknum; taskid++);
    if(taskid >= tasknum){
	    bios_putstr("INPUT ERROR");
	    return 0;
    }
    int memAdress     = TASK_MEM_BASE + (taskid) * TASK_SIZE;
    int blockAdress   = memAdress - tasks[taskid].task_offset % 512;
    int blockid_start = tasks[taskid].task_offset / 512;
    int blockid_end   = tasks[taskid].task_addr_end / 512;

    bios_sdread(blockAdress, blockid_end - blockid_start + 1, blockid_start);

    return memAdress;*/
    
    //[p1-task5]
    int memAdress     = TASK_MEM_BASE + (taskid - 1) * TASK_SIZE;
    int appAdress   = memAdress + tasks[taskid-1].task_offset % 512;
    int blockid_start = tasks[taskid-1].task_offset / 512;
    int blockid_end   = tasks[taskid-1].task_addr_end / 512;

    bios_sdread(memAdress, blockid_end - blockid_start + 1, blockid_start);

    return appAdress;





    // return 0;
}
